package thread;

/**
 * 重排序导致的乱序现象测试
 * jdk6会出现
 * jdk7不会出现
 * jdk8不会出现
 */
public class PossibleReordering {

    public int x = 0, y = 0;
    public int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            final PossibleReordering reordering = new PossibleReordering();
            Thread one = new Thread(new Runnable() {
                public void run() {
                    reordering.a = 1;
                    reordering.x = reordering.b;
                }
            });

            Thread other = new Thread(new Runnable() {
                public void run() {
                    reordering.b = 1;
                    reordering.y = reordering.a;
                }
            });
            other.start();
            one.start();
            one.join();
            other.join();
            if(reordering.x == 0 && reordering.y == 0){
                System.out.println(reordering.x + "," + reordering.y);
            }
        }
        System.out.println(System.currentTimeMillis() - startTime);
    }
}